@echo off
chcp 65001
setlocal EnableDelayedExpansion

:: ========== 配置区 ==========
set UNITY_PATH="D:\Unity Hub\Hub_Install\2023.2.20f1c1\Editor\Unity.exe"
set PROJECT_PATH="D:\Project\HotFix_Test\HybridCLR_Project"
set LOG_FILE="%PROJECT_PATH%\compile_log.txt"
set SOURCE_DLL="%PROJECT_PATH%\HybridCLRData\HotUpdateDlls\StandaloneWindows64\HotUpdate.dll"
set TARGET_DIR="%PROJECT_PATH%\Build\HybridCLR_ProJect_Data\StreamingAssets"
set TARGET_FILE="%TARGET_DIR%\HotUpdate.dll.bytes"
set GAME_EXE="%PROJECT_PATH%\Build\HybridCLR_Project.exe"

:: 询问是否关闭Unity
echo 开始编译HybridCLR DLL之前先关闭Unity
:ASK_CLOSE
set /p CLOSE_CHOICE="是否关闭Unity进程？(y/n): "
if /i "%CLOSE_CHOICE%" == "y" (
    echo 正在关闭Unity进程...
    taskkill /F /IM Unity.exe >nul 2>&1
    if errorlevel 1 (
        echo Unity进程未运行
    ) else (
        echo Unity进程已关闭
    )
    timeout /t 2 /nobreak
) else if /i "%CLOSE_CHOICE%" == "n" (
    echo 继续执行...
) else (
    echo 请输入 y 或 n
    goto ASK_CLOSE
)

:: 编译DLL
echo 开始编译HybridCLR DLL...
%UNITY_PATH% -batchmode -nographics -projectPath %PROJECT_PATH% -executeMethod AutoCompileHybridCLR.AutoCompile -quit

:: 检查编译是否成功（这里通过检查源DLL文件是否存在来判断）
if exist %SOURCE_DLL% (
    echo HybridCLR DLL编译成功！
    
    :: 创建目标目录（如果不存在）
    if not exist %TARGET_DIR% mkdir %TARGET_DIR%
    
    :: 复制文件
    copy /Y %SOURCE_DLL% %TARGET_FILE%
    echo 文件已复制到: %TARGET_FILE%
    
    :: 打开目标文件夹
    explorer %TARGET_DIR%
    
    :: 运行游戏
    if exist %GAME_EXE% (
        echo 启动游戏...
        start "" %GAME_EXE%
    ) else (
        echo 错误：游戏执行文件不存在！
    )
) else (
    echo 编译失败，请查看日志文件
    explorer %PROJECT_PATH%
)

echo.
echo 处理完成，按任意键退出...
pause >nul